/*
 * @lc app=leetcode.cn id=2 lang=javascript
 *
 * [2] 两数相加
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function(l1, l2) {
  return addTwoNumbersByLinkNode(l1,l2);
}

/**
 * 链表
 * @param {LinkNode} l1 
 * @param {LinkNode} l2 
 */
function addTwoNumbersByLinkNode(l1,l2) {
  let carry = 0;
  let head = null;
  let temp = null;
  let value = 0;
  let x = null;
  while(l1 || l2) {
    let r = (l1?l1.val:0) + (l2?l2.val:0) + carry;
    value = r%10;
    carry = r>=10?1:0;
    if(head==null) {
      head = new ListNode(value);
    }else {
      /**
       * 内存大，运行时间短
       */
      if(head.next) {
        temp.next = new ListNode(value);
        temp = temp.next;
      }else{
        head.next = new ListNode(value);
        temp = head.next;
      }
      /**
       * 使用内存少，但是运行时间长
       */
      // x = temp||head;
      // x.next = new ListNode(value);
      // temp = x.next; 
    }
    l1 = l1?l1.next:null;
    l2 = l2?l2.next:null;
  }
  //最高一位有进位的场景
  if(carry===1) {
    //为了只有一位是temp是空的,例如:[5][5]
    (temp||head).next = new ListNode(1);
  }
  return head;
}
/**
 * 使用数组来做运算，最后统一转成链表
 * 我一开始没有Get到需要用链表的意思，过程都用了数组
 * 上官网看评论才知道这题需要链表
 * @param {LinkNode} l1rev 
 * @param {LinkNode} l2rev 
 */
function addTwoNumbersByArray(l1rev,l2rev) {
  let carry = 0;
  let resultAry = [];
  while(l1rev || l2rev) {
    let r = (l1rev?l1rev.val:0) + (l2rev?l2rev.val:0) + carry;
    if(r>=10) {
      carry = 1;
      resultAry.push(r-10);
    }else{
      carry = 0;
      resultAry.push(r);
    }
    l1rev = l1rev?l1rev.next:null;
    l2rev = l2rev?l2rev.next:null;
  }
  if(carry===1) {
    resultAry.push(carry);
  }
}
/**
 * 链表转化数组
 * @param {LinkNode} linkNode 链表
 * @returns Array
 */
function link2Array(linkNode) {
  let result = [];
  while (linkNode) {
    result.push(linkNode.val);
    linkNode = linkNode.next;
  }
  return result;
}

/**
 * 将数组转化为链表
 * @param {Array} number 
 * @returns LinkNode
 */
function transLink(number) {
  let head = null;
  let temp = null;
  for(let i = 0;i<number.length;i++) {
    if(head==null) {
      head = new ListNode(number[i],null);
    } else {
      if(head.next) {
        temp.next = new ListNode(number[i],null); 
        temp = temp.next;
      }else{
        temp = new ListNode(number[i],null); 
        head.next = temp;
      }
    }
  }
  return head;
}

/**
 * 链表模型
 * @param {Number} val 值
 * @param {LinkNode} next 指向下一个节点
 */
function ListNode(val, next) {
  this.val = (val===undefined ? 0 : val)
  this.next = (next===undefined ? null : next)
}
/**
 * 将链表翻转
 * @param {LinkNode} linkNode 
 * @returns 
 */
 function reverseLink(linkNode) {
  return transLink(link2Array(linkNode).reverse());
}
//console.log(linkToInt(transLink([2,4,3])));
// console.log(
//   JSON.stringify(addTwoNumbers(
//     transLink([2,4,3]),transLink([5,6,4])))
// );
// @lc code=end

